package models;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import play.db.DB;

public class Setor {
	int IDSetor;
	String NomeSetor;
	List<Funcionario> listaFuncionario;
	
	public Setor(){
		listaFuncionario = new ArrayList<Funcionario>();
	}
	
	public boolean cadastrarSetor(String NomeSetor) throws SQLException{
		ResultSet st = DB.getConnection().createStatement().executeQuery("select NomeSetor from Setor where NomeSetor like '%"+NomeSetor+"%'");
		String s1 = null;
		@SuppressWarnings("unused")
		int r = -1;
		
		while(st.next()){
			s1 = st.getString("NomeSetor");
		}		
		
		if(NomeSetor.equalsIgnoreCase(s1)){
			return false;
		} else {
			setNomeSetor(s1);
			r = DB.getConnection().createStatement().executeUpdate("insert into Setor(NomeSetor) values('"+NomeSetor+"')");
			return true;
		}
	}
	
	public List<String> listarSetores() throws SQLException{
		List<String> listaSetor = new ArrayList<String>();
		
		 ResultSet st = DB.getConnection().createStatement().executeQuery("select NomeSetor from Setor");
		 
		 while(st.next()){
			  listaSetor.add(st.getString("NomeSetor"));
		  }
		 
		 return listaSetor;
	}
	
	public void alterarSetor(String AntigoSetor, String NovoSetor) throws SQLException{
		@SuppressWarnings("unused")
		int r = DB.getConnection().createStatement().executeUpdate("update Setor\n set NomeSetor='"+NovoSetor+"' where NomeSetor like '%"+AntigoSetor+"%'");
	}
	
	public void removerSetor(String NomeSetor) throws SQLException{
		@SuppressWarnings("unused")
		int r = DB.getConnection().createStatement().executeUpdate("delete from Setor where NomeSetor like '%"+NomeSetor+"%'");
	}
	
	public int getIDSetor() {
		return IDSetor;
	}
	public void setIDSetor(int iDSetor) {
		IDSetor = iDSetor;
	}
	public String getNomeSetor() {
		return NomeSetor;
	}
	public void setNomeSetor(String nomeSetor) {
		NomeSetor = nomeSetor;
	}
	
}
